package 领扣算法.A简单题.找到最接近0的数字;

import java.util.Comparator;
import java.util.PriorityQueue;

public class Main {
    public int findClosestNumber(int[] nums) {
        PriorityQueue<Integer> data=new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Math.abs(o1)-Math.abs(o2);
            }
        });
        for (int num : nums) {
            data.add(num);
        }
        int min=Math.abs(data.peek());
        int max=data.poll();
        while (!data.isEmpty())
        {
            int middle=data.poll();
            if(min==Math.abs(middle)){
                min=Math.abs(middle);
                max=Math.max(max,middle);
            }
            else
                break;
        }
        return max;
    }
}
